<?php
// Classes

/* 


Mods
-----------------------------------------------------
 2013-03-04 CT: added comments
*/

/**
 * CF_ArrayBySpec is a class handle re-arranging the columns in an array to match the order specified
 * in the filespec. It will also call column modificaiton (makefuncion) functions on data as specified
 * @package createfastlib
 * @author Chris Tyler
 * @version 1.0
 * @ is the base class for the an extensable HTML table class, and data provider classes, for array based data. 
 * @copyright 2007-2008 
 */

 class CF_ArrayBySpec {
 	/**
	* @var array $valuesarray A 2D array of values, an array of associative row arrays, valuesarray[<row number>][<column heading>]
	*/
	var $valuesarray;
	/**
	* @var array $currentrow holds the current associative array row being processed.  Use this to access other values in the same row in make functions.
	*/	
	var $currentrow;
	/**
	* @var array $fieldspec Array describes data and formatting options
	*/	
	var $fieldspec;
	/**
	* @var string $row_status Internal variable for determing odd or even row in table
	* If no filespec exists, create one using the spec_from _firstrow function
	*/	
	function __construct($values_array = '', $the_spec = ''){
		$this->values_array = $values_array;
		if(is_array($the_spec)){
			$this->fieldspec = $the_spec;
			}
		else{
			$this->fieldspec = $this->spec_from_firstrow();
			}	
			
	}
	
	private function spec_from_firstrow(){
		if(is_array($this->values_array[0])){
			foreach($this->values_array[0] AS $column=>$value){
				$fieldspec[$column]['show'] = 1;
			}
		}
		return $fieldspec;
	}
	
	
// This will take the raw array from the database and arrange it and format it according
	// to fieldspec adjustments
	function fill_spec_array(){
		if(is_array($this->values_array)){	
		foreach($this->values_array AS $rownum=>$this->currentrow){
			foreach($this->fieldspec AS $column=>$spec){
				$makefunction = 'make_' . $column;
				if(!empty($spec['makefunction'])){
					$makefunction = $spec['makefunction'];
				}
				if(method_exists($this,$makefunction)){	
					$data[$rownum][$column] = $this->$makefunction($this->currentrow[$column]);
				}	else{
					$data[$rownum][$column] = $this->currentrow[$column];				
				}			
			} // end foreach spec
		}	// end foreach row
		}
		$this->valuesarray = $data;	
		return $data;
	} // end fill_spec_array	
	
		
	
}
?>